package com.qf.controller;

import com.qf.entity.po.User;
import com.qf.entity.vo.UserVO;
import com.qf.service.UserService;
import com.qf.util.ResultCode;
import com.qf.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RequestMapping("user")
@CrossOrigin
@Controller
@Slf4j
@Api(tags = {"用户模块"})
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("test")
    @ResponseBody
    @ApiOperation(value = "查询全部用户列表",notes = "查询全部用户列表信息")
    public Object test() {
        return userService.selectUser();
    }

    /**
     * #TODO 跳转到列表页面
     * @return
     */
    @GetMapping("toUserList")
    @ApiOperation(value = "跳转到用户列表页面",notes = "跳转到用户列表页面并携带用户信息")
    public String toUserList(HttpServletRequest request){
        List<User> users = userService.selectUser();
        request.setAttribute("list",users);
        return "user/list";
    }

    /**
     * #TODO 根据id删除用户信息
     * @param id
     * @return
     */
    @GetMapping("deleteUserById")
    @ResponseBody
    @ApiOperation(value = "根据id删除用户信息",notes = "根据id删除用户信息，并返回删除数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value="用户id",required = true,dataType = "int",defaultValue = "100")
    })
    public int deleteUserById(@RequestParam("id") final int id){
        int i = userService.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * @TODO 根据id查询用户信息
     * @param id
     * @return
     */
    @RequestMapping(value = "selectUserById")
    @ResponseBody
    @ApiOperation(value = "根据id查询用户信息",notes = "可以使用任何方式，返回用户信息的json数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value="用户id",required = true,dataType = "int",defaultValue = "100")
    })
    public Object selectUserById(@RequestParam("id") final int id){

        if (id>100){
            throw new RuntimeException("id大于1000");
        }

        User user = userService.selectByPrimaryKey(id);
        if (user==null){
            return new ResultJson<>(null, ResultCode.NOT_DATA);
        }
        return new ResultJson<>(user);
    }

    /**
     * #TODO 添加用户
     * @param userVO
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "姓名",required = true,dataType = "String"),
            @ApiImplicitParam(name = "age",value = "年龄",required = true,defaultValue = "18",dataType = "int"),
            @ApiImplicitParam(name = "sex",value = "性别(0女/1男)",required = true,defaultValue = "0",dataType = "int"),
            @ApiImplicitParam(name = "status",value = "状态(0无效,1有效,2白名单,3锁定)",required = true,dataType = "int"),
            @ApiImplicitParam(name = "remark",value = "备注",dataType = "String")
    })
    @RequestMapping("addUserAjax")
    @ResponseBody
    @ApiOperation(value = "添加用户",notes = "添加用户并返回到用户列表页面，以json的方式")
    public Object addUserAjax(@Valid UserVO userVO, HttpServletRequest request){
        //UserVO转User
        User user = new User();
        user.setName(userVO.getName());
        user.setSex(userVO.getSex());
        user.setAge(userVO.getAge());
        user.setRemark(userVO.getRemark());
        user.setStatus(userVO.getStatus());
        user.setCreateTime(new Date());
        int num = userService.insertSelective(user);
        log.info("用户信息添加成功{}",user);
        return new ResultJson<>(user,ResultCode.SUCCESS);
    }


    /**
     * #TODO 进入添加页面
     * @param request
     * @return
     */
    @GetMapping("toAdd")
    @ApiOperation(value = "跳转到用户添加页面",notes = "跳转到用户添加页面")
    public String toAdd(HttpServletRequest request){
        return "user/add";
    }

    /**
     * #TODO 添加用户
     * @param user
     * @return
     */
    @PostMapping("addUser")
    @ApiOperation(value = "添加用户",notes = "添加用户并返回到用户列表页面")
    public String addUser(User user,HttpServletRequest request){

        String time = request.getParameter("time");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = dateFormat.parse(time);
            user.setCreateTime(parse);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        int num = userService.insertSelective(user);
        log.info("用户信息添加成功{}",num);
        return this.toUserList(request);
    }

}
